package com.taptech.doufu.ui.fragment;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.taptech.doufu.R;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.adapter.ChoiceCategoryAdapter;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.bean.DFHomeCategoryDetailBean;
import com.taptech.doufu.bean.DFHomeCategoryGson;
import com.taptech.doufu.bean.base.BaseResponseBean;
import com.taptech.doufu.ui.view.WaitDialog;
import com.taptech.doufu.ui.view.topicview.MeasuredGridLayoutManager;

import java.util.List;

public class ChoiceCategoryFragment extends DiaobaoBaseFragment {
    public static final String INTENT_KEY_LOAD_DATA_NOW = "intent_key_load_data_now";
    private RecyclerView mRecyclerView;
    private ChoiceCategoryAdapter adapter;
    private String url;
    private boolean isInit;
    private WaitDialog waitDialog;


    public static ChoiceCategoryFragment newInstance(String url,boolean loadDataNow) {
        ChoiceCategoryFragment fragment = new ChoiceCategoryFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Constant.URL, url);
        bundle.putBoolean(INTENT_KEY_LOAD_DATA_NOW,loadDataNow);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_choice_category, null);
        mRecyclerView = view.findViewById(R.id.mRecyclerView);
        Bundle bundle = getArguments();
        url = bundle.getString(Constant.URL);
        boolean loadDataNow = bundle.getBoolean(INTENT_KEY_LOAD_DATA_NOW);
        if (loadDataNow) {
            initData();
        }
        return view;
    }

    private void setData(List<DFHomeCategoryDetailBean> list) {
        adapter = new ChoiceCategoryAdapter(getContext(),list);
        MeasuredGridLayoutManager manager = new MeasuredGridLayoutManager(getContext(),3);
        manager.setScrollEnabled(true);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(adapter);
    }


    private void dismissDialog() {
        if (waitDialog != null && waitDialog.isShowing()) {
            waitDialog.dismiss();
        }
    }


    public void initData() {
        if (isInit){
            return;
        }
        if (waitDialog == null) {
            waitDialog = new WaitDialog(getActivity(), R.style.updateDialog);
        }
        waitDialog.show();
        isInit = true;
        ApiClient.getInstance().getService().getChoiceCategoryDetail(url)
                .compose(RxJavaHelper.<BaseResponseBean<DFHomeCategoryGson>>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<BaseResponseBean<DFHomeCategoryGson>>(){
                    @Override
                    public void onNext(BaseResponseBean<DFHomeCategoryGson> res) {
                        super.onNext(res);
                        if (res != null && res.getData()!=null) {
                            List<DFHomeCategoryDetailBean> list = res.getData().getList();
                            setData(list);
                        }
                        dismissDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        dismissDialog();
                    }
                });
    }
}
